<?php

namespace app\backend\controller\sys;

use app\common\controller\Basic;
use app\common\service\hrm\CompanyService;
use app\common\service\sys\DictService;
use app\common\service\sys\UserService;

/**
 * 系统字典
 * @author kaige
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 * @update 2022-06-08
 */
class Dict extends Basic
{

  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new DictService();
  }

  /**
   * 列表 -- Layui treeTable 特殊树型
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      url_decode($data);
      $result = $this->service->_list($data, false);
      $tree = [];
      if ($result && $result['count'] > 0) {
        $tree = \app\common\util\TreeUtil::list_to_tree($result['data'], 0, 'id', 'pid', 'children');
        \app\common\util\TreeUtil::tree_add_field($tree);
      }
      return json(_layui($result['count'], $tree));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   * @return array|string
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      if(isset($data['company_id']) && empty($data['company_id'])){
        return _jerr('必须选择所属单位');
      }
      return json($this->service->saveData($data));
    }
    if ($this->request->isGet()) {
      $data = [
        'companyList' => CompanyService::getCompanyList(),
        'userInfo' => $this->user,
        'dictPid' => $this->service->getListAll(['pid' => 0])
      ];
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   * @return array|string
   */
  public function edit()
  {
    $id = input('id/d', 0);
    if ($id === 0) {
      return _jerr(1022);
    }
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      if(isset($data['company_id']) && empty($data['company_id'])){
        return _jerr('必须选择所属单位');
      }
      //当前是修改，就需要判断是否会陷入死循环
      if ($this->service->checkDie($data['id'], $data['pid']) === false) {
        return _jerr('上一级选错了吧，会陷入死循环的');
      }
      return json($this->service->saveData($data));
    }
    if ($this->request->isGet()) {
      $data = $this->service->getInfoById($id);
      $data['companyList'] = CompanyService::getCompanyList();
      $data['userInfo'] = $this->user;
      $data['dictPid'] = $this->service->getListAll(['pid' => 0]);
      if (isset($data['handle_time'])) {
        $data['handle_time'] = time2str($data['handle_time']);
      }
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   */
  protected function _form_filter(&$data)
  {
    url_decode($data);
    if (isset($data['id']) && isset($data['pid']) && $data['id'] == $data['pid']) { //不允许 id =pid
      $data['pid'] = 0;
    }
    if (!isset($data['id'])) {
      $data['handle_uid'] = $this->user['id'];
      $data['handle_time'] = time();
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      if ($id === 0) {
        return _jerr(1002);
      }
      return json($this->service->_del($id));
    }
    return _jerr(1000);
  }
}
